package host

import (
	"net/http"
	"strings"

	"gitee.com/go-course/go8/projects/devcloud/cmdb/apps/resource"
	"github.com/infraboard/mcube/http/request"
)

const (
	AppName = "host"
)

type Service interface {
	RPCServer
}

func NewHostSet() *HostSet {
	return &HostSet{
		Items: []*Host{},
	}
}

func (s *HostSet) Add(item *Host) {
	s.Items = append(s.Items, item)
}

func (s *HostSet) Length() int {
	return len(s.Items)
}

func (s *HostSet) UpdateResource(rss ...*resource.Resource) {
	for m := range rss {
		rid := rss[m].Meta.ResourceId
		for i := range s.Items {
			if s.Items[i].Resource.Meta.ResourceId == rid {
				s.Items[i].Resource = rss[m]
			}
		}
	}
}

func NewDefaultHost() *Host {
	return &Host{
		Resource: resource.NewDefaultResource(resource.TYPE_HOST),
		Spec:     &Spec{},
	}
}

func NeWQueryHostRequest() *QueryHostRequest {
	return &QueryHostRequest{
		Page: request.NewDefaultPageRequest(),
	}
}

func NewQueryHostRequestFromHTTP(r *http.Request) *QueryHostRequest {
	req := NeWQueryHostRequest()
	qs := r.URL.Query()
	req.ResourceIds = strings.Split(qs.Get("resource_ids"), ",")
	req.Page = request.NewPageRequestFromHTTP(r)
	return req
}
